package com.currency.mall.ui.my;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.currency.mall.Constant;
import com.currency.mall.R;
import com.currency.mall.entity.UserInfo;
import com.currency.mall.netWork.NetWork;
import com.mifengs.commonbase.base.BaseActivity;
import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.commonbase.util.StatusBarCompat;
import com.mifengs.commonbase.util.StatusBarUtils;
import com.mifengs.commonbase.util.ToastUitl;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/12/25.
 */

public class UserInfoActivity extends BaseActivity {
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_sfz_code)
    TextView tvSfzCode;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.ll_name)
    LinearLayout llName;
    @BindView(R.id.ll_sfz)
    LinearLayout llSfz;
    private boolean isAuto =false;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        ButterKnife.bind(this);
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(getContext(), R.color.title_color));
        StatusBarUtils.StatusBarLightMode(this);
        isAuto = getIntent().getBooleanExtra("isAuto",false);
        init();
    }
    private void init(){
        if(isAuto){
            llName.setVisibility(View.GONE);
            llSfz.setVisibility(View.GONE);
        }else{
            llName.setVisibility(View.VISIBLE);
            llSfz.setVisibility(View.VISIBLE);
        }
        getUserInfo();
    }

    public void getUserInfo() {
        NetWork.getUserApi().userInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(UserInfo userInfo) {
                        if (userInfo.getCode().equals(Constant.SUCCESS_CODE)) {
                            PreferenceUtils.saveUserName(getContext(), userInfo.getRealName());
                            tvName.setText(userInfo.getRealName());
                            tvSfzCode.setText(userInfo.getIdCard().substring(0, 1) + "************" + userInfo.getIdCard().substring(userInfo.getIdCard().length() - 1, userInfo.getIdCard().length()));
                            tvPhone.setText(userInfo.getMobile().substring(0, 4) + "****" + userInfo.getMobile().substring(userInfo.getMobile().length() - 3, userInfo.getMobile().length()));
                        } else {
                            ToastUitl.show(userInfo.getMsg(), 200);
                        }
                    }
                });
    }


    @OnClick(R.id.ll_back)
    public void onViewClicked() {
        finish();
    }

    @OnClick({R.id.ll_name, R.id.ll_sfz,R.id.ll_back})
    public void onViewClicked(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.ll_name:
                 intent = new Intent(getContext(),NameAutoActivity.class);
                 intent.putExtra("type",1);
                 startActivityForResult(intent,1000);
                break;
            case R.id.ll_sfz:
                intent = new Intent(getContext(),NameAutoActivity.class);
                startActivityForResult(intent,1000);
                break;
            case R.id.ll_back:
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == 1001){
            llName.setVisibility(View.GONE);
            llSfz.setVisibility(View.GONE);
            getUserInfo();
        }
    }
}
